/*
 * Copyright (c) 2010, artur
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * kiddie-os
 * Created on: 14.04.2010
 *     Author: artur
 *
 */

#ifndef LIST_
#define LIST_

namespace util
{

	typedef struct l_el
	{
			struct l_el *prev;
			struct l_el *next;
			void *data;
	} __list_element;

	template<typename T>
	class list
	{
		public:
			list();
			~list()
			{
			}
			int add(T);
			int remove(T);
			void clear(void);
			T next(void);
			__list_element *find_prev(T);
			int size; // Number of elements on Stack
			void rew(void);
			void fwd(void);

			/*
			 * FIXME Hide this attributes
			 * TODO USE  iterator here!!!!!!!1
			 */
			// protected:
		public:
			__list_element *head;
			__list_element *end;
			__list_element *pos;
	};

	template<typename T>
	list<T>::list()
	{
		pos = end = head = 0;
		size = 0;
	}

	/**
	 * @fn int list<T>::add(T t)
	 * @brief Adds new element to the tail of list.
	 */
	template<typename T>
	int list<T>::add(T t)
	{
		__list_element * elem = new __list_element;
		elem->data = (void *) t;

		if (!head)
		{
			pos = end = head = elem;
			head->next = head->prev = head;
		}
		else
		{
			elem->next = end->next;
			elem->prev = end;
			elem->next->prev = elem;
			elem->prev->next = elem;
			end = elem;
		}

		size++;
	}

	/**
	 * @fn void list<T>::rew()
	 * @brief Places current position to the head of list.
	 */
	template<typename T>
	void list<T>::rew()
	{
		pos = head;
	}

	/**
	 * @fn void list<T>::fwd()
	 * @brief Places current position to the end of list.
	 */
	template<typename T>
	void list<T>::fwd()
	{
		pos = end;
	}

	/**
	 * @fn T list<T>::next(void)
	 * @brief Returns an element which is next to pos in list.
	 *
	 * @return T
	 */
	template<typename T>
	T list<T>::next(void)
	{
		//	if (pos != end)
		//	{
		pos = pos->next;
		//	}
		return (T) (pos->data);
	}

	/**
	 * @fn __list_element *list<T>::find_prev(T d)
	 * @brief Seeks for first element with data equals to 'd'.
	 *
	 * @return __list_element pointer
	 */
	template<typename T>
	__list_element *list<T>::find_prev(T d)
	{
		bool found = false;
		__list_element *_el = pos;
		do
		{
			if (!_el)
				break;
			if (_el->data == d)
			{
				found = true;
				break;
			}
			_el = _el->next;
		} while (_el != pos);

		return found ? _el : NULL;
	}

	/**
	 * @fn int list<T>::remove(T t)
	 * @brief Removes element from list.
	 *
	 * @return 0 on success or -1 on error.
	 */
	template<typename T>
	int list<T>::remove(T t)
	{
		__list_element *el = find_prev(t);

		if (!el)
			return -1;

		__list_element *prev = el->prev;
		__list_element *next = el->next;

		// remove el
		prev->next = next;
		next->prev = prev;

		if (head == el)
			head = next;
		if (end == el)
			end = prev;
		if (pos == el)
			pos = next;

		size--;
		if (size <= 0)
			head = pos = end = 0;
		delete el;

		return 0;
	}

	/**
	 * @fn void list<T>::clear()
	 * @brief Clears the list. Removes all list elements.
	 *
	 */
	template<typename T>
	void list<T>::clear()
	{
	}

}

#endif /* LIST_ */
